<?php
// +----------------------------------------------------------------------
// | 功能说明"
// +----------------------------------------------------------------------
// | Copyright (c) 2016~2030 http://www.jmzh1688.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: lxl
// | jmzhfsauto
// +----------------------------------------------------------------------
namespace app\admin\model;
use Think\Db;
use think\Model;

class TrackxaxisModel extends Model{
    protected $name = 'track_xaxis';
    protected $autoWriteTimestamp = true;//时间自动更新 create_time ,update_time

    /**
     * 获取x轴数据
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function get_data($where,$field='*',$orders=['id'=>'asc']){
        $where['isdelete'] = 0;
        $res = $this->where($where)->field($field)->order($orders)->select();
        foreach ($res as $k=>$v){
            $res[$k] = $v->toarray();
        }
        return $res;
    }

    /**
     *
     */
    public function get_level($id){
        return $this->field('level')->find($id)['level'];
    }

    public function edit($param){
        try{
            $result =  $this->update($param);
            if(false === $result){
                return msg(-1, '', $this->getError());
            }else{
                return msg(1,url('Trackxaxis/index'), '修改横轴数据成功');
            }
        }catch(PDOException $e){
            return msg(-2, '', $e->getMessage());
        }
    }


    public function add($param){
        try{
            $param['isdelete'] = 0;
            if($param['parentid'] == 0){
                $param['level'] = 1;
            }else{
                $level = $this->where('id',$param['parentid'])->value('level');
                $param['level'] = $level+1;
            }
            $result =  $this->save($param);
            if(false === $result){
                return msg(-1, '', $this->getError());
            }else{
                return msg(1,url('Trackxaxis/index'), '添加横轴项成功');
            }
        }catch(PDOException $e){
            return msg(-2, '', $e->getMessage());
        }
    }
    public function getById($id){
        return $this->where('id', $id)->find();
    }

    public function delById($id){
        try{
            $this->where('id', $id)->update(['isdelete'=>'1']);
            return msg(1, '', '删除成功');
        }catch( PDOException $e){
            return msg(-1, '', $e->getMessage());
        }
    }

    public function getActionWhere($where){
        return $this->where($where)->find();
    }

    public function getListbyRoleId($roleid){
        $para['isdelete']=0;
        $role=Db::name('sys_role')->where('id',$roleid)->find();
        if($roleid==0||$roleid==1){
            $result =  $this->where($para)->order('ordernum asc')->select();
            return $result;
        }
        if(!empty($role)){
            $menut=  config('database.prefix')."sys_role";

            $idstr=" FIND_IN_SET(id , (select menulist from ".$menut." where id=".$roleid."))";
            $result =  $this->where($para)->where($idstr)->order('ordernum asc')->select();

            $t=$this->getLastSql();
            return $result;

        }
        return null;
    }


}
